package com.zlll.winner.service.mall;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlll.winner.business.model.mall.MallSeckillGoods;
import com.zlll.winner.business.model.mall.MallSeckillManage;
import com.zlll.winner.business.service.mall.IMallGoodsDetailsService;
import com.zlll.winner.business.service.mall.IMallSeckillGoodsService;
import com.zlll.winner.business.vo.mall.MallGoodsDetailsVo;
import com.zlll.winner.business.vo.mall.MallSeckillVo;
import com.zlll.winner.mapper.mall.MallSeckillGoodsMapper;
import com.zlll.winner.mapper.mall.MallSeckillManageMapper;
import com.zlll.winner.business.service.mall.IMallSeckillManageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 商品秒杀 管理 服务实现类
 * </p>
 *
 * @author qsx
 * @since 2019-10-08
 */
@Service
public class MallSeckillManageServiceImpl extends ServiceImpl<MallSeckillManageMapper, MallSeckillManage> implements IMallSeckillManageService {

    @Autowired
    private MallSeckillManageMapper mallSeckillManageMapper;
    @Autowired
    private IMallSeckillGoodsService mallSeckillGoodsService;
    @Autowired
    private IMallGoodsDetailsService mallGoodsDetailsService;

    @Override
    public Object getSeckillList() {
        LambdaQueryWrapper<MallSeckillManage> queryWrapper = Wrappers.lambdaQuery();
        Long currentTimeMillis = System.currentTimeMillis();
        queryWrapper.le(MallSeckillManage::getStartTime, currentTimeMillis);
        queryWrapper.ge(MallSeckillManage::getEndTime, currentTimeMillis);
        List<MallSeckillManage> list = mallSeckillManageMapper.selectList(queryWrapper);
        if (list.size() > 0) {
            MallSeckillManage mallSeckillManage = list.get(0);
            Map result = new HashMap();
            result.put("statTime", mallSeckillManage.getStartTime());
            result.put("endTime", mallSeckillManage.getEndTime());
            result.put("seckillNo", mallSeckillManage.getSeckillNo());

            List<MallGoodsDetailsVo> goodsDetailsVos = mallSeckillManageMapper.getMallGoodsDetailsList();
            result.put("list", goodsDetailsVos);

            return result;
        }
        return null;
    }

    @Override
    public IPage<MallSeckillVo> getMallSeckillByCondition(Long time, String goodsName, Integer pageSize, Integer pageNo) {
        IPage<MallSeckillVo> page = new Page<>(pageNo, pageSize);
        List<MallSeckillVo> list = mallSeckillManageMapper.getMallSeckillByCondition(time, goodsName, page);
        page.setRecords(list);
        return page;
    }

    public String generateSeckillNo() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String data = simpleDateFormat.format(new Date().getTime());
        Integer sj = (int) ((Math.random() * 9 + 1) * 1000);
        return data + sj;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MallSeckillManage add(Long startTime, Long endTime, List<Map<String, Object>> externals, Long userId) {
        Long time = System.currentTimeMillis();
        MallSeckillManage mallSeckillManage = new MallSeckillManage();
        mallSeckillManage.setCreateUser(userId.toString());
        mallSeckillManage.setCreateTime(time);
        mallSeckillManage.setUpdateTime(time);
        mallSeckillManage.setStartTime(startTime);
        mallSeckillManage.setEndTime(endTime);
        mallSeckillManage.setSeckillNo(generateSeckillNo());
        save(mallSeckillManage);

        List<MallSeckillGoods> goodsList = new ArrayList<>();
        for (Map map : externals) {
            MallSeckillGoods goods = new MallSeckillGoods();
            goods.setNum(Integer.parseInt(map.get("num").toString()));
            goods.setMaxNum(Integer.parseInt(map.get("maxNum").toString()));
            goods.setSeckillId(mallSeckillManage.getId());
            goods.setCreateTime(time);
            goods.setUpdateTime(time);
            //goods.setResidueNum(Integer.parseInt( map.get("num").toString()));
            goods.setGoodsId(Long.parseLong(map.get("goodsId").toString()));
            goodsList.add(goods);
            Integer status = map.get("status") == null ? null : Integer.parseInt(map.get("status").toString());
            if(status != null && !status.equals(2)){
                mallGoodsDetailsService.setPutaway(goods.getGoodsId());
            }
        }
        mallSeckillGoodsService.saveBatch(goodsList);
        return mallSeckillManage;
    }

    @Override
    public List<MallSeckillManage> isrepeat(Long startTime, Long endTime) {
        List<MallSeckillManage> list = mallSeckillManageMapper.getMallSeckillManageByTime(startTime, endTime);
        return list;
    }

    @Override
    public MallSeckillManage getMallSeckillManageById(Long id) {

        MallSeckillManage mallSeckillManage = mallSeckillManageMapper.selectById(id);
        mallSeckillManage.setMallSeckillVos(mallSeckillManageMapper.getMallSeckillBySeckillId(id));
        return mallSeckillManage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MallSeckillManage edit(Long id, Long startTime, Long endTime, List<Map<String, Object>> externals, Long userId) {
        Long time = System.currentTimeMillis();

        MallSeckillManage mallSeckillManage = mallSeckillManageMapper.selectById(id);
        mallSeckillManage.setEndTime(endTime);
        mallSeckillManage.setStartTime(startTime);
        mallSeckillManage.setUpdateTime(time);
        mallSeckillManage.setUpdateUser(userId.toString());

        List<MallSeckillGoods> goodsList = new ArrayList<>();

        for (Map map : externals) {
            MallSeckillGoods goods = new MallSeckillGoods();
            Long id2 = map.get("id") == null ? null : Long.parseLong(map.get("id").toString());
            if (id2 != null) {
                goods.setId(id2);
                goods.setUpdateUser(userId.toString());
                goods.setUpdateTime(time);
            } else {
                goods.setCreateTime(time);
                goods.setCreateUser(userId.toString());
            }
            goods.setNum(Integer.parseInt(map.get("num").toString()));
            goods.setMaxNum(Integer.parseInt(map.get("maxNum").toString()));
            goods.setSeckillId(mallSeckillManage.getId());
            //goods.setResidueNum(Integer.parseInt( map.get("num").toString()));
            goods.setGoodsId(Long.parseLong(map.get("goodsId").toString()));
            goodsList.add(goods);
            Integer status = map.get("status") == null ? null : Integer.parseInt(map.get("status").toString());

            if(status != null && !status.equals(2)){
                mallGoodsDetailsService.setPutaway(goods.getGoodsId());
            }
        }


        LambdaQueryWrapper<MallSeckillGoods> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(MallSeckillGoods::getSeckillId, id);
        queryWrapper.eq(MallSeckillGoods::getIsDel, 0);
        List<MallSeckillGoods> mallSeckillGoods = mallSeckillGoodsService.list(queryWrapper);
        List<MallSeckillGoods> deleteList = new ArrayList<>();

        for (MallSeckillGoods goods : mallSeckillGoods) {
            Boolean isDel = true;
            for (MallSeckillGoods seckillGoods : goodsList) {
                if (seckillGoods.getId() != null && goods.getId().equals(seckillGoods.getId())) {
                    isDel = false;
                    break;
                }
            }

            if (isDel) {
                goods.setIsDel(1);
                goods.setUpdateUser(userId.toString());
                goods.setUpdateTime(time);
                deleteList.add(goods);
            }
        }

        if (deleteList.size() > 0) {

            mallSeckillGoodsService.updateBatchById(deleteList);
        }

        //新增和修改
        mallSeckillGoodsService.saveOrUpdateBatch(goodsList);


        return mallSeckillManage;
    }

    @Override
    public Boolean deleteMallSeckillManage(String seckillNo, Long userId) {
        LambdaQueryWrapper<MallSeckillManage> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(MallSeckillManage::getIsDel,0);
        wrapper.eq(MallSeckillManage::getSeckillNo,seckillNo);
//        //设置需要修改的值
        MallSeckillManage mallSeckillManage = new MallSeckillManage();
        mallSeckillManage.setUpdateUser(userId.toString());
        mallSeckillManage.setIsDel(1);
        mallSeckillManage.setUpdateTime(System.currentTimeMillis());

        return update(mallSeckillManage,wrapper);


//        List<MallSeckillManage> mallSeckillManages = mallSeckillManageMapper.selectList(wrapper);
//        for (MallSeckillManage mallSeckillManage:mallSeckillManages) {
//            mallSeckillManage.setUpdateUser(userId.toString());
//            mallSeckillManage.setIsDel(1);
//            mallSeckillManage.setUpdateTime(System.currentTimeMillis());
//        }
//
//        return updateBatchById(mallSeckillManages);

    }
}
